package com.molichuxing.gateway.bff.basic.services.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.molichuxing.framework.contants.RedisKeyConst;
import com.molichuxing.framework.dto.UploadFileRequestDto;
import com.molichuxing.framework.utils.RedisUtil;
import com.molichuxing.gateway.bff.basic.convertor.Convert;
import com.molichuxing.gateway.bff.basic.services.FileStorageBffService;
import com.molichuxing.services.infrastructure.dto.response.FileStorageConfigDto;
import com.molichuxing.services.infrastructure.service.FileStorageConfigService;

/**
 * 文件存储bff
 * 
 * @author zoumingyu
 * @date 2019年9月5日
 *
 */
@Service("fileStorageBffService")
public class FileStorageBffServiceImpl implements FileStorageBffService {


	@Resource
	private RedisUtil redisUtil;

	@Resource
	private FileStorageConfigService fileStorageConfigService;

	/**
	 * 根据类型（归属文件夹）查询
	 * 
	 * @param folder
	 * @return
	 */
	@Override
	public UploadFileRequestDto getFileStorageConfig(String folder) {
		UploadFileRequestDto result = null;
		// 先查询redis
		String fileStorageConfig = redisUtil.get(RedisKeyConst.FILE_STORAGE_KEY + folder);
		if (fileStorageConfig != null) {
			result = JSONObject.parseObject(fileStorageConfig, UploadFileRequestDto.class);
			return result;
		}
		FileStorageConfigDto fileStorageConfigDto = fileStorageConfigService.getFileStorageConfig(folder);
		if (fileStorageConfigDto != null) {
			// 存储到redis
			result = Convert.toUploadFileRequestDto(fileStorageConfigDto);
			redisUtil.set(RedisKeyConst.FILE_STORAGE_KEY + folder, JSONObject.toJSONString(result));
		}

		return result;
	}
}
